package com.example.ipcapplication

import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.IBinder
import android.util.Log
import android.view.View
import com.example.ipcapplication.aidl.IServer

class MainActivity : AppCompatActivity() {

    private var iServer: IServer? = null

    private var mConnect = object : ServiceConnection {
        override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
            iServer = IServer.Stub.asInterface(service)
            Log.d("test", "onServiceConnected")

            try {
                val i = iServer?.testFunction("test String")
                Log.i("test", "after testFunction a: $i");
            } catch (e: Exception) {
            }
        }

        override fun onServiceDisconnected(name: ComponentName?) {
            Log.i("test", "onServiceDisconnected");
        }

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }

    fun testAIDL(view: View) {
        val intent = Intent(this, RemoteTestService::class.java)
        bindService(intent, mConnect, Context.BIND_AUTO_CREATE)
    }
}